package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.R.drawable;
import qeb.handheldmedical.adapter.ConsultationAdapter.ViewHolder;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年8月31日 下午8:15:35
 * @declaration
 */
public class AppointmentStudyListAdapter extends BaseAdapter {

	private static final String TAG = "jsonArray";

	private int mCount;
	private Context mContext;
	private JSONArray jsonArray = null;

	public AppointmentStudyListAdapter(int count, Context context,
									   JSONArray jsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.jsonArray = jsonArray;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_appointment_study_item, null);
			viewholder = new ViewHolder();
			viewholder.tvCourseName = (TextView) convertView
					.findViewById(R.id.tvCourseName);
			viewholder.tvStudyTime = (TextView) convertView
					.findViewById(R.id.tvStudyTime);
			viewholder.tvPeopleNo = (TextView) convertView
					.findViewById(R.id.tvPeopleNo);

			viewholder.tvStudyState = (TextView) convertView
					.findViewById(R.id.tvStudyState);
			convertView.setTag(viewholder);

		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		try {
			JSONObject jobj = jsonArray.getJSONObject(position);
			if (jobj != null) {
				viewholder.tvCourseName.setText(jobj.getString("LEARNNAME"));
				viewholder.tvStudyTime.setText(jobj.getString("LEARNTIME"));
				viewholder.tvPeopleNo.setText(jobj.getString("LEARNNUMBER")
						+ "/" + jobj.getString("TOTALNUMBER"));
				viewholder.tvStudyState.setText(jobj.getString("ISBM"));
				if (jobj.getString("ISBM").indexOf("已报名") > 0) {
					convertView.setBackgroundColor(Color.GREEN);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, e.toString());
		}
		return convertView;
	}

	static class ViewHolder {
		TextView tvCourseName;
		TextView tvStudyTime;
		TextView tvPeopleNo;
		TextView tvStudyState;
	}
}
